<!DOCTYPE html>



  


<html class="theme-next muse use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/blog/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/blog/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/blog/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/blog/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Spring,">










<meta name="description" content="Spring里面Bean是一个什么概念？简单来讲，Bean指的就是一个POJO类，也就是具有一些属性以及setter和getter方法的类。广义地讲，一切的java类都可以视作Bean，这些类都可以通过Spring容器管理，发现，注入。 什么是BeanSpring Bean是被实例的，组装的及被Spring 容器管理的Java对象。 Spring 容器会自动完成@Bean对象的实例化。 创建应用对">
<meta name="keywords" content="Spring">
<meta property="og:type" content="article">
<meta property="og:title" content="spring学习-Bean与元数据配置">
<meta property="og:url" content="http://vosamo.gitee.io/blog/2019/07/31/spring学习-Bean与元数据配置/index.html">
<meta property="og:site_name" content="VOSAMO">
<meta property="og:description" content="Spring里面Bean是一个什么概念？简单来讲，Bean指的就是一个POJO类，也就是具有一些属性以及setter和getter方法的类。广义地讲，一切的java类都可以视作Bean，这些类都可以通过Spring容器管理，发现，注入。 什么是BeanSpring Bean是被实例的，组装的及被Spring 容器管理的Java对象。 Spring 容器会自动完成@Bean对象的实例化。 创建应用对">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="http://vosamo.gitee.io/blog/2019/07/31/spring学习-Bean与元数据配置/config.png">
<meta property="og:updated_time" content="2019-08-05T01:29:42.183Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="spring学习-Bean与元数据配置">
<meta name="twitter:description" content="Spring里面Bean是一个什么概念？简单来讲，Bean指的就是一个POJO类，也就是具有一些属性以及setter和getter方法的类。广义地讲，一切的java类都可以视作Bean，这些类都可以通过Spring容器管理，发现，注入。 什么是BeanSpring Bean是被实例的，组装的及被Spring 容器管理的Java对象。 Spring 容器会自动完成@Bean对象的实例化。 创建应用对">
<meta name="twitter:image" content="http://vosamo.gitee.io/blog/2019/07/31/spring学习-Bean与元数据配置/config.png">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/blog/',
    scheme: 'Muse',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://vosamo.gitee.io/blog/2019/07/31/spring学习-Bean与元数据配置/">





  <title>spring学习-Bean与元数据配置 | VOSAMO</title>
  








</head>

<body itemscope="" itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope="" itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/blog/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">VOSAMO</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/blog/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/blog/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br>
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/blog/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br>
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/blog/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            归档
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope="" itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://vosamo.gitee.io/blog/blog/2019/07/31/spring学习-Bean与元数据配置/">

    <span hidden itemprop="author" itemscope="" itemtype="http://schema.org/Person">
      <meta itemprop="name" content="vosamo">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blog/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope="" itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="VOSAMO">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">spring学习-Bean与元数据配置</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-07-31T15:49:32+08:00">
                2019-07-31
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope="" itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/笔记/" itemprop="url" rel="index">
                    <span itemprop="name">笔记</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>Spring里面Bean是一个什么概念？简单来讲，Bean指的就是一个POJO类，也就是具有一些属性以及setter和getter方法的类。广义地讲，一切的java类都可以视作Bean，这些类都可以通过Spring容器管理，发现，注入。</p>
<h2 id="什么是Bean"><a href="#什么是Bean" class="headerlink" title="什么是Bean"></a>什么是Bean</h2><p>Spring Bean是被实例的，组装的及被Spring 容器管理的Java对象。</p>
<p>Spring 容器会自动完成@Bean对象的实例化。</p>
<p>创建应用对象之间的协作关系的行为称为：<strong>装配(wiring)</strong>，这就是依赖注入的本质。</p>
<p>需要向Spring容器提供相关信息，以便实例化Bean并指定如何对这些Bean进行装配，所提供的的信息被称为<strong>配置元数据</strong>。</p>
<a id="more"></a>
<h2 id="配置元数据的三种方式"><a href="#配置元数据的三种方式" class="headerlink" title="配置元数据的三种方式"></a>配置元数据的三种方式</h2><h3 id="基于XML的元数据配置"><a href="#基于XML的元数据配置" class="headerlink" title="基于XML的元数据配置"></a>基于XML的元数据配置</h3><p>配置元数据的传统格式是XML，然而XML并不是唯一的配置元数据的方式。通过在XML文件中定义Bean的属性，具体用法如下：</p>
<ul>
<li>beans标签中定义了一组bean，每个bean用<code>&lt;bean&gt;&lt;/bean&gt;</code>标签包括</li>
<li>bean标签中定义了一个bean的具体属性和依赖关系</li>
</ul>
<p>XML格式一般如下：</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?xml version="1.0" encoding="UTF-8" ?&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">beans</span></span></span><br><span class="line">&lt;!--标准命名空间--&gt;</span><br><span class="line">xmlns="http://www.springframework.org/schema/beans"</span><br><span class="line"><span class="comment">&lt;!--xsi标准命名空间，用于指定自定义命名空间的Schema文件--&gt;</span></span><br><span class="line">xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"</span><br><span class="line"><span class="tag">&lt;<span class="name">!—aop表示自定义命名空间，aop是该命名空间的简称，而后面是命名空间的全称。必须在xsi命名空间为其指定的命名空间对应的Schema文件--</span>&gt;</span></span><br><span class="line">    xmlns:aop="http://www.springframework.org/schema/aop"</span><br><span class="line"> </span><br><span class="line"><span class="tag">&lt;<span class="name">!—下面这四行代码，是为每个命名空间指定具体的schema文件--</span>&gt;</span></span><br><span class="line">    xsi:schemaLocation="</span><br><span class="line">           http://www.springframework.org/schema/beans</span><br><span class="line">    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd</span><br><span class="line">           http://www.springframework.org/schema/aop</span><br><span class="line">           http://www.springframework.org/schema/context/spring-aop-3.0.xsd"&gt;</span><br><span class="line"><span class="tag">&lt;<span class="name">!—默认命名空间的配置</span> <span class="attr">--</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">bean</span> <span class="attr">id</span> = <span class="string">"app"</span> <span class="attr">class</span>=<span class="string">"cn.lovepi.App"</span> /&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">!—aop命名空间的配置</span> <span class="attr">--</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">aop:config</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">aop:pointcut</span> <span class="attr">id</span>=<span class="string">"mycut"</span> <span class="attr">expression</span>=<span class="string">"execution(* cn.love*(..))"</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">aop:config</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">beans</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>示例：</p>
<p>定义元数据配置XML文件：</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?xml version="1.0" encoding="UTF-8"?&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">beans</span> <span class="attr">xmlns</span>=<span class="string">"http://www.springframework.org/schema/beans"</span></span></span><br><span class="line"><span class="tag">	<span class="attr">xmlns:xsi</span>=<span class="string">"http://www.w3.org/2001/XMLSchema-instance"</span></span></span><br><span class="line"><span class="tag">	<span class="attr">xsi:schemaLocation</span>=<span class="string">"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">bean</span> <span class="attr">id</span>=<span class="string">"student"</span> <span class="attr">class</span>=<span class="string">"com.demo.Student"</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"student_name"</span> <span class="attr">value</span>=<span class="string">"liming"</span>/&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"course"</span> <span class="attr">ref</span>=<span class="string">"course"</span>/&gt;</span></span><br><span class="line">	<span class="tag">&lt;/<span class="name">bean</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">bean</span> <span class="attr">id</span>=<span class="string">"course"</span> <span class="attr">class</span>=<span class="string">"com.demo.Course"</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"course_name"</span> <span class="attr">value</span>=<span class="string">"English"</span>/&gt;</span></span><br><span class="line">	<span class="tag">&lt;/<span class="name">bean</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">beans</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>上面的XML文件中定义了两个bean：student和course，并且前者依赖后者，所以在student的property属性中引用了course。</p>
<p>分别创建Student类和Course类</p>
<p>Student:</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Student</span> </span>&#123;</span><br><span class="line">    <span class="keyword">private</span> String student_name;</span><br><span class="line">    <span class="keyword">private</span> Course course;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setStudent_name</span><span class="params">(String student_name)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.student_name = student_name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">getStudent_name</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> student_name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setCourse</span><span class="params">(Course course)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.course = course;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> Course <span class="title">getCourse</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> course;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>Course:</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Course</span> </span>&#123;</span><br><span class="line">    <span class="keyword">private</span> String course_name;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setCourse_name</span><span class="params">(String course_name)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.course_name = course_name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">getCourse_name</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> course_name;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>创建一个测试类：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Main</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">		ClassPathXmlApplicationContext applicationContext = <span class="keyword">new</span> ClassPathXmlApplicationContext(<span class="string">"/com/demo/beans.xml"</span>);</span><br><span class="line">		Student stu = applicationContext.getBean(<span class="string">"student"</span>,Student.class);</span><br><span class="line">		System.out.println(<span class="string">"Student name :"</span> + stu.getStudent_name()+<span class="string">"course: "</span>+stu.getCourse().getCourse_name());</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>一个项目中往往有多个XML配置文件，Spring加载多个XML配置文件的方法有：</p>
<ul>
<li>将多个配置文件以数组形式传给ApplicationContext</li>
<li>在一个配置文件中使用import导入所需要的其他配置文件</li>
</ul>
<p>方式1：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">ApplicationContext context = <span class="keyword">new</span> ClassPathXmlApplicationContext(<span class="keyword">new</span> String[]</span><br><span class="line">    &#123;<span class="string">"Spring-Common.xml"</span>,<span class="string">"Spring-Connection.xml"</span>,<span class="string">"Spring-ModuleA.xml"</span>&#125;);</span><br></pre></td></tr></table></figure>
<p>方式2：</p>
<p>定义一个新的XML配置文件：Spring-All-Module.xml，文件中import了其他配置文件</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">beans</span> <span class="attr">.....</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">import</span> <span class="attr">resource</span>=<span class="string">"common/Spring-Common.xml"</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">import</span> <span class="attr">resource</span>=<span class="string">"connection/Spring-Connection.xml"</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">import</span> <span class="attr">resource</span>=<span class="string">"module/Spring-Module.xml"</span>/&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">beans</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>在代码中只需要加载Spring-All-Module.xml这个配置文件即可：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">ApplicationContext context = <span class="keyword">new</span> ClassPathXmlApplicationContext(“Spring-All-Module.xml”);</span><br></pre></td></tr></table></figure>
<h3 id="基于JAVA的元数据配置"><a href="#基于JAVA的元数据配置" class="headerlink" title="基于JAVA的元数据配置"></a>基于JAVA的元数据配置</h3><p>基于JAVA类定义Bean配置元数据，其实就是通过JAVA类定义Spring配置元数据。具体步骤如下：</p>
<ul>
<li>使用<strong>@Configuration</strong>注解需要作为配置的类，表示该类将定义Bean的元数据</li>
<li>使用<strong>@Bean</strong>注解相应的方法，该<strong>方法名默认就是Bean的名称</strong>，该方法返回值就是Bean的对象</li>
<li><strong>AnnotationConfigApplicationContext</strong>或子类进行加载基于java类的配置</li>
</ul>
<p>示例：</p>
<p>首先创建一个配置类：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Configuration</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ApplicationContextConfig</span> </span>&#123;</span><br><span class="line">    <span class="meta">@Bean</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">message</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">"hello"</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>然后创建一个测试类，测试配置是否成功：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ConfigurationTest</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        AnnotationConfigApplicationContext ctx =</span><br><span class="line">        <span class="keyword">new</span> AnnotationConfigApplicationContext(ApplicationContextConfig.class);</span><br><span class="line">        System.out.println(ctx.getBean(<span class="string">"message"</span>));</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>通过@Configuration注解的类作为配置类使用，表示该类中将定义Bean配置元数据，且使用@Configuration注解的类本身也是一个Bean，使用方法如下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Configuration</span>(<span class="string">"ctxConfig"</span>)</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ApplicationContextConfig</span> </span>&#123;</span><br><span class="line">    ……</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>其中Configuration中的<strong>参数值</strong>即为该bean的<strong>名称</strong>。</p>
<p>通过<strong>@Bean</strong>注解配置类中的相应方法，则该方法名默认就是Bean名，该方法返回值就是Bean对象，并定义了Spring IoC容器如何实例化、自动装配、初始化Bean逻辑，具体使用方法如下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Bean</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> String <span class="title">message</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">new</span> String(<span class="string">"hello"</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>以上代码等价于XML配置：</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">bean</span> <span class="attr">id</span>=<span class="string">"message"</span> <span class="attr">class</span>=<span class="string">"java.lang.String"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">constructor-arg</span> <span class="attr">index</span>=<span class="string">"0"</span> <span class="attr">value</span>=<span class="string">"hello"</span>/&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">bean</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p><strong>注意</strong>：<strong>使用bean注解的方法不能是private、final、static的。</strong></p>
<h3 id="基于注解的元数据配置"><a href="#基于注解的元数据配置" class="headerlink" title="基于注解的元数据配置"></a>基于注解的元数据配置</h3><p>Spring 2.0引入了基于注解的配置方式，即Bean的定义信息可以通过在Bean的实现类上标注注解实现。</p>
<p><strong>@Component</strong>是Spring容器中的基本注解，表示容器中的一个组件（<strong>bean</strong>），可以作用在任何层次，下面的示例介绍该注解的使用方法。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Component</span>(<span class="string">"userDao"</span>)</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">userDao</span></span>&#123;......&#125;</span><br></pre></td></tr></table></figure>
<p>等效于XML配置</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">bean</span> <span class="attr">id</span>=<span class="string">"userDao"</span> <span class="attr">class</span>=<span class="string">"cn.lovepi.***.userDao"</span>/&gt;</span></span><br></pre></td></tr></table></figure>
<p>Spring在2.5后提供了一个<strong>context</strong>的命名空间，它提供了通过扫描<strong>类包</strong>来加载利用注解定义的Bean的方式。</p>
<p>在context中可以使用<strong>resource-pattern</strong>来过滤出特定的类。</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">context:component-scan</span> <span class="attr">base-package</span>=<span class="string">"cn.lovepi.spring"</span> <span class="attr">resource-pattern</span>=<span class="string">"anno/*.class"</span>/&gt;</span></span><br></pre></td></tr></table></figure>
<p>默认情况下加载的是package下的*.class即扫描全部类，在使用了resource-pattern之后，则只扫描package下的anno子包下的所有类。不过使用resource-pattern并不能提供给我们完善的功能，所有我们得使用<strong>过滤子元素</strong>的方法。</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">context:component-scan</span> <span class="attr">base-package</span>=<span class="string">"cn.lovepi.spring"</span>&gt;</span></span><br><span class="line">   <span class="tag">&lt;<span class="name">context:include-filter</span> <span class="attr">type</span>=<span class="string">"regex"</span> <span class="attr">expression</span>=<span class="string">"cn.lovepi.spring.*"</span>/&gt;</span></span><br><span class="line">   <span class="tag">&lt;<span class="name">context:exclude-filter</span> <span class="attr">type</span>=<span class="string">"aspectj"</span> <span class="attr">expression</span>=<span class="string">"cn.lovepi..*Controller+"</span>/&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">context:component-scan</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>其中，<strong>include-filter</strong>表示要包含的目标类，<strong>exclude-filter</strong>表示要排除在外的目标类。一个<strong>component-scan</strong>标签下可以有<strong>多个</strong>include-filter和exclude-filter。</p>
<p>Spring3.0提供了一系列的针对依赖注入的注解，这使得Spring IoC在XML文件之外多了一种可行的选择，主要包含如下注解类型：</p>
<ul>
<li><strong>Bean的定义注解</strong></li>
<li><strong>Bean的生命周期注解</strong></li>
<li><strong>Bean的依赖检查注解</strong></li>
<li><strong>Bean的自动装配注解</strong></li>
</ul>
<h4 id="Bean的定义注解"><a href="#Bean的定义注解" class="headerlink" title="Bean的定义注解"></a><strong>Bea</strong>n的定义注解</h4><p><strong>@Repository</strong>注解用于将<strong>数据访问层</strong>（DAO层）的类标识为Spring Bean。</p>
<p>①首先使用@Repository注解将DAO类标识为Bean</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Repository</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">UserDaoImpl</span> <span class="keyword">implements</span> <span class="title">UserDao</span></span>&#123;......&#125;</span><br></pre></td></tr></table></figure>
<p>②在XML文件中配置扫描</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">beans</span> <span class="attr">...</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">context:component-scan</span> <span class="attr">base-package</span>=<span class="string">"cn.lovepi.dao"</span>/&gt;</span></span><br><span class="line">    ......</span><br><span class="line"><span class="tag">&lt;<span class="name">beans</span>/&gt;</span></span><br></pre></td></tr></table></figure>
<p>这样就不需要在XML文件中显式地定义bean的配置，Spring容器在初始化的时候便会自动扫描base-package所指定的包以及子包下面的所有class文件。所有标注为Repository的类将被自动注册为bean。</p>
<p><strong>Repository只能标注在DAO类上面</strong>，这是因为该注解的作用不只是将类识别为bean，同时他还能将所标注的类中所抛出的<strong>数据访问异常</strong>封装为<strong>Spring的数据访问异常类型</strong>。Spring本身提供了一个丰富的，并且是与具体的访问技术无关的数据访问异常结构，用于封装不同的持久层框架所抛出的异常，使得<strong>异常独立与底层的框架。</strong></p>
<p>Spring2.5在@Repository的基础上增加了功能类似的额外三个注解，总共有如下四种注解：</p>
<ul>
<li>@Component：一个泛化的概念，表示一个组件（Bean），可作用在任何层次</li>
<li>@Controller：用于对Controller实现类进行标注，扩展自Component注解</li>
<li>@Service：用于对Service实现类进行标注，扩展自Component注解</li>
<li>@Repository：用于对DAO实现类进行标注，扩展自Component注解</li>
</ul>
<h4 id="Bean的生命周期注解"><a href="#Bean的生命周期注解" class="headerlink" title="Bean的生命周期注解"></a>Bean的生命周期注解</h4><p><strong>Spring1.x</strong>为此提供了两种方式供用户<strong>指定执行生命周期回调</strong>的方法：</p>
<ul>
<li>实现Spring提供的两个接口：initializingBean 和 DisposableBean，这种方法是要求bean类实现Spring的接口，但增加了bean和Spring容器的耦合度，因此不推荐使用。</li>
<li>在XML文件中使用<code>&lt;bean&gt;</code>的init-method 和 destory-method 属性，指定初始化之后和回调之前的回调方法。这两个属性的取值是bean中相应的初始化和销毁方法的名称。方法名称任意，但是方法不能有参数。</li>
</ul>
<p>例如：</p>
<figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">&lt;bean id=<span class="string">"userService"</span> <span class="class"><span class="keyword">class</span></span>=<span class="string">"cn.lovepi.***.UserService"</span></span><br><span class="line">   init-method=<span class="string">"init"</span> destory-method=<span class="string">"destory"</span>&gt;</span><br><span class="line">&lt;/bean&gt;</span><br></pre></td></tr></table></figure>
<p>我们指定了userService 这个bean的初始化方法为：init     销毁方法为：destory。</p>
<p><strong>Spring2.5</strong>在保留以上两种方式的基础上，定义了两个用于指定声明周期方法的注解：</p>
<ul>
<li><strong>@PostConstruct</strong>：初始化之后的执行的回调方法</li>
<li><strong>@PreDestroy</strong>：销毁之前的回调方法</li>
</ul>
<p>示例：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">PersonService</span></span>&#123;</span><br><span class="line">   <span class="meta">@PostConstruct</span></span><br><span class="line">   <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">init</span><span class="params">()</span></span>&#123;......&#125;</span><br><span class="line">   <span class="meta">@PreDestory</span></span><br><span class="line">   <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">destory</span><span class="params">()</span></span>&#123;......&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>在这里init方法是初始化之后执行的方法，而destory方法为销毁之前执行的方法。由于使用了注解，所以得<strong>激活Bean的后处理器</strong>，所以得在XML配置文件当中增加</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">context:annotation-config</span>/&gt;</span></span><br></pre></td></tr></table></figure>
<h4 id="Bean的依赖检查注解"><a href="#Bean的依赖检查注解" class="headerlink" title="Bean的依赖检查注解"></a>Bean的依赖检查注解</h4><p>Spring2.0之前使用dependency-check在配置文件中设置属性用于依赖检查（只会检查Setter方法是否被调用），缺点是粒度较粗，该属性的取值包括以下几种：</p>
<ul>
<li>none： 默认不执行依赖检查</li>
<li>simple ：对原始基本类型和集合类型进行检查</li>
<li>objects ：对复杂类型进行检查</li>
<li>all ：对所有类型进行检查</li>
</ul>
<p>使用Spring2.0提供的@Required注解，提供了更细粒度的控制，@Required注解只能标注在Setter方法之上，（标注在其他方法之上会被忽略 ）用于检查其是否被调用，当Setter方法未被调用的话会抛出异常。由于使用了注解，所以得激活Bean的后处理器，所以得在XML配置文件当中增加</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">context:annotation-config</span>/&gt;</span></span><br></pre></td></tr></table></figure>
<h4 id="Bean的自动装配注解"><a href="#Bean的自动装配注解" class="headerlink" title="Bean的自动装配注解"></a>Bean的自动装配注解</h4><p>有时没有必要显式地在Bean定义中定义依赖项，可以让Spring容器自动地向Bean中注入依赖项。<strong>@Autowired</strong>可以对成员变量、方法和构造函数进行标注，来完成自动装配的工作，他根据<strong>类型</strong>进行自动装配，如果需要按<strong>名称</strong>进行装配，则需要配合<strong>@Qualifier</strong>使用。</p>
<p>当标注了@Autowired的方法所需的<strong>类型</strong>在Spring容器中不存在的话会<strong>抛出异常</strong>。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Service</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">LoginService</span></span>&#123;</span><br><span class="line">   <span class="meta">@Autowired</span></span><br><span class="line">   <span class="keyword">private</span> LogDao logDao;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>假如Spring容器中没有LogDao这个bean的话便会抛出异常。解决的办法便是使用<strong>required=false</strong>属性来标注。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">LoginService</span></span>&#123;</span><br><span class="line">   <span class="meta">@Autowired</span>(required=<span class="keyword">false</span>)</span><br><span class="line">   <span class="keyword">private</span> LogDao LogDao;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>但是假如Spring当中存在多个所需类型的bean，那么便要使用<strong>@Qualifier</strong>注解来指定名称。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">LoginService</span></span>&#123;</span><br><span class="line">   <span class="meta">@Autowired</span></span><br><span class="line">   <span class="meta">@Qualifier</span>(<span class="string">"userDao"</span>)</span><br><span class="line">   <span class="keyword">private</span> UserDao userDao;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><strong>@Autowired</strong> 可以对类中<strong>集合类</strong>的变量或方法入参进行标注,此时会将容器中<strong>类型匹配的所有Bean</strong>都注入进来，如下所示：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">loginService</span></span>&#123;</span><br><span class="line">   <span class="meta">@Autowired</span>(required=<span class="keyword">false</span>)</span><br><span class="line">   <span class="keyword">public</span> List&lt;Plugin&gt; pligins;</span><br><span class="line">   <span class="function"><span class="keyword">public</span> List&lt;Plugin&gt; <span class="title">getPlugins</span><span class="params">()</span></span>&#123;</span><br><span class="line">      <span class="keyword">return</span> plugins;</span><br><span class="line">   &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>Spring会将容器中所有类型为Plugin的bean都注入到集合中去。</p>
<h2 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h2><p>不同配置方式的比较：</p>
<img src="/blog/2019/07/31/spring学习-Bean与元数据配置/config.png">
<p>Spring支持的三种元数据配置方式有各自的适用场景：</p>
<ul>
<li><p>基于XML的配置主要使用场景：</p>
<p>第三方类库，如DataSource、JdbcTemplate等；</p>
<p>命名空间，如aop、context等；</p>
</li>
<li><p>基于注解的配置主要使用场景：</p>
<p>Bean的实现类是当前项目开发的，可直接在Java类中使用注解配置</p>
</li>
<li><p>基于Java类的配置主要使用场景</p>
<p>对于实例化Bean的逻辑比较复杂，则比较适合用基于Java类配置的方式</p>
</li>
</ul>
<p>在日常的开发中我们主要是使用XML配置和注解配置方式向结合的开发方式，一般不推荐使用基于Java类的配置方式。</p>
<p><strong>参考资料</strong>：</p>
<p><a href="https://blog.csdn.net/icarus_wang/article/details/51649635" target="_blank" rel="noopener">Spring学习（二十二） Bean配置的三种方式（XML、注解、Java类）介绍与对比</a></p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/blog/tags/Spring/" rel="tag"># Spring</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/blog/2019/07/30/SpringBoot集成MyBatis和mybatis-generator/" rel="next" title="SpringBoot集成MyBatis和mybatis-generator">
                <i class="fa fa-chevron-left"></i> SpringBoot集成MyBatis和mybatis-generator
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/blog/2019/08/09/修复项目不一致导致的查询不到虚机IP信息问题/" rel="prev" title="修复项目不一致导致的查询不到虚机IP信息问题">
                修复项目不一致导致的查询不到虚机IP信息问题 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope="" itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image" src="/blog/images/avatar.gif" alt="vosamo">
            
              <p class="site-author-name" itemprop="name">vosamo</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/blog/archives/">
              
                  <span class="site-state-item-count">10</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/blog/categories/index.html">
                  <span class="site-state-item-count">4</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/blog/tags/index.html">
                  <span class="site-state-item-count">7</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#什么是Bean"><span class="nav-number">1.</span> <span class="nav-text">什么是Bean</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#配置元数据的三种方式"><span class="nav-number">2.</span> <span class="nav-text">配置元数据的三种方式</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#基于XML的元数据配置"><span class="nav-number">2.1.</span> <span class="nav-text">基于XML的元数据配置</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#基于JAVA的元数据配置"><span class="nav-number">2.2.</span> <span class="nav-text">基于JAVA的元数据配置</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#基于注解的元数据配置"><span class="nav-number">2.3.</span> <span class="nav-text">基于注解的元数据配置</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#Bean的定义注解"><span class="nav-number">2.3.1.</span> <span class="nav-text">Bean的定义注解</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#Bean的生命周期注解"><span class="nav-number">2.3.2.</span> <span class="nav-text">Bean的生命周期注解</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#Bean的依赖检查注解"><span class="nav-number">2.3.3.</span> <span class="nav-text">Bean的依赖检查注解</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#Bean的自动装配注解"><span class="nav-number">2.3.4.</span> <span class="nav-text">Bean的自动装配注解</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#总结"><span class="nav-number">3.</span> <span class="nav-text">总结</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">vosamo</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Muse</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/blog/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/blog/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/motion.js?v=5.1.4"></script>



  
  

  
  <script type="text/javascript" src="/blog/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/blog/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/blog/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
